{layout name="../../admin/view/layout/main" /}
{:breadcrumb(['数据备份(轻量版):xbak/index/index', '备份列表'])}
<div class="card">
    <div class="card-body">
        <div id="toolbar" class="toolbar-btn-action">
            <button type="button" class="btn btn-success me-1 btn-sm" onclick="add();">
                <span class="mdi mdi-plus"></span> 新增备份
            </button>
            <button type="button" class="btn btn-default me-1 btn-sm" onclick="clearCache();">
                <span class="mdi mdi-delete-outline"></span> 清理缓存{if $files}({$files}){/if}
            </button>
        </div>
        <table id="table"></table>
    </div>
</div>

<script>
    var layerIndex;
    var result = '';
    function add(id){
        var curr = layer.confirm("大数据量请用专业的备份软件！<br/>数据备份时，请勿关闭页面，否则备份失败。", {
            btn: ['开始备份', '取消'] ,
            icon: 'warning'
        }, function(){
            layer.close(curr);
            layerIndex = open_layer('备份数据库', "{:url('xbak/index/backup')}?iframe=1", function (){
                $('#table').bootstrapTable('refresh');
            }, ['300px', '300px']);
        });
    }
    function close(){
        layer.close(layerIndex);
        xp_notify(result , 'success');
    }
    function clearCache(){
        xp_confirm("{:url('xbak/index/clear')}", {}, function (){
            location.reload();
        });
    }
    // 下载
    function download(name){
        location.href = "{:url('xbak/index/download')}?name=" + name;
    }
    function remove(name){
        xp_confirm("{:url('xbak/index/remove')}", {name: name}, function (){
            $('#table').bootstrapTable('refresh');
        });
    }
    function recover(name){
        var curr = layer.confirm("数据还原时，请勿关闭页面，否则还原失败。", {
            btn: ['开始还原', '取消'] ,
            icon: 'warning'
        }, function(){
            layer.close(curr);
            layerIndex = open_layer('还原数据库', "{:url('xbak/index/recover')}?name=" + name + "&iframe=1", function (){
                $('#table').bootstrapTable('refresh');
            }, ['300px', '300px']);
        });
    }
    $(function (){
        $('#table').bootstrapTable({
            toolbar: '#toolbar',
            classes: 'table table-bordered table-hover table-striped lyear-table',
            url: "{:url('xbak/index/data')}",
            pagination: false,
            search: false,
            sidePagination: 'server',
            columns: [
                {'field': 'name', title: '文件名'},
                {'field': 'size', title: '文件大小'},
                {'field': 'time', title: '备份时间'},
                {'field': 'actions', title: '操作', formatter: function(value, item){
                    var dom = `<span class="badge bg-info" onclick="download('${item.name}');" style="cursor: pointer;"><span class="mdi mdi-download"></span> 下载</span>`;
                    dom = dom + `<span class="badge bg-success" onclick="recover('${item.name}');" style="cursor: pointer; margin-left: 5px;"><span class="mdi mdi-undo"></span> 还原</span>`;
                    dom = dom + `<span class="badge bg-danger" onclick="remove('${item.name}');" style="cursor: pointer; margin-left: 5px;"><span class="mdi mdi-delete-outline"></span> 删除</span>`;
                    return dom;
                }},
            ]
        });
    })
</script>